/*
 * Created by SharpDevelop.
 * User: rseward
 * Date: 6/4/2008
 * Time: 6:04 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Data;

using RD.Metadata.Database.Sql;

namespace AvvaMobileNHibernateHelperKit
{
	/// <summary>
	/// SqlServerDriver for NHibernate Helper Kit.
	/// </summary>
	public class SqlServerDriver : BaseDatabaseDriver
	{
		public SqlServerDriver()
		{
		}
		
		public override void LoadTypeSqlToNetMap()
		{
			typeSqlToNetMap["int"] = "Int32";
			typeSqlToNetMap["tinyint"] = "Int32";
			typeSqlToNetMap["smallint"] = "Int32";
			typeSqlToNetMap["bigint"] = "Int32";
			typeSqlToNetMap["char"] = "String";
			typeSqlToNetMap["varchar"] = "String";
			typeSqlToNetMap["nchar"] = "String";
			typeSqlToNetMap["nvarchar"] = "String";
			typeSqlToNetMap["datetime"] = "DateTime";
			typeSqlToNetMap["smalldatetime"] = "DateTime";
			typeSqlToNetMap["money"] = "Decimal";
			typeSqlToNetMap["decimal"] = "Decimal";
			typeSqlToNetMap["smallmoney"] = "Decimal";
			typeSqlToNetMap["bit"] = "Boolean";
			typeSqlToNetMap["uniqueidentifier"] = "Guid";
			typeSqlToNetMap["text"] = "String";
			typeSqlToNetMap["ntext"] = "String";
		}
		
		public override NHibernate.property GetHbmFieldProperty(SqlMetaColumn col, string propName, string colName)
		{
			NHibernate.property xmlProp = new NHibernate.property();
			
			string netType = GetNetType( col.DbType );

			xmlProp.name=propName;
			xmlProp.column1 = colName;
			xmlProp.type1 = netType;
			
			switch (col.DbType)
			{
				case DbType.DateTime:
				case DbType.Date:
				case DbType.Currency:
				case DbType.Decimal:
				case DbType.Binary:
					break;
					
				case DbType.Guid:
					xmlProp.length = "30";
					break;

					// TODO: handle the text cases.
					/*
				case "text":
					prop = "<property name=\"" + propName + "\" column=\"" + colName + "\" type=\"String\" />";
					break;
				case "ntext":
					"<property name=\"" + propName + "\" column=\"" + colName + "\" type=\"String\" />";
					break; */
				default:
					if (col.Length>0) {
						xmlProp.length = ""+col.Length;
					}
					break;
			}
			
			
			return xmlProp;
		}
		
		
	}
}
